package cn.jdy.plugins.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.Advice;
import cn.jdy.plugins.entity.Comment;


/***
 * 
 * @author Administrator
 * @desc   评论实现类
 */
@Service
public class CommentService extends BaseDao<Comment> {
    
	
	
	/**
	 * 按条件查询
	 * @param Notice
	 * @return
	 */
	public List<Comment> query(Comment comment, String... date) {
		if(comment == null) return super.findAll();
		if(comment.getiDisplayLength() > 0){
		  return super.find(createFilter(comment, date),comment.getiDisplayStart(),comment.getiDisplayLength());
		}
		return super.find(createFilter(comment, date));
	}

	public Long count(Comment comment, String... date) {
		return super.count(createFilter(comment, date));
	}
	
	/****
	 * @desc 分页
	 * @param comm
	 * @return
	 */
	public List<Comment> querypage(Comment comm) {
		return super.find(createFilter(comm), comm.getPageNo(),5);
	}
	
	public Long count(Comment comment) {
		return super.count(createFilter(comment));
	}
	/**
	 * 拼装条件
	 * @param Notice
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(Comment comment, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	 
	  if(!StringUtil.isNullOrEmpty(comment.getStatus())){
	    dc.add(Restrictions.eq("status", comment.getStatus()));
	  }
      if(!StringUtil.isNullOrEmpty(comment.getCommentContent())){
    	dc.add(Restrictions.like("commentContent", comment.getCommentContent().trim(), MatchMode.ANYWHERE));
      }
      if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
  	    dc.add(Restrictions.between("createTime", date[0],date[1]));
  	  }
      if(!StringUtil.isNullOrEmpty(comment.getNewsId())){
  	    dc.add(Restrictions.eq("newsId", comment.getNewsId()));
  	  }
	  dc.addOrder(Order.desc("createTime"));
		return dc;
	}
	
}
